<template>
  <div class="message">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="main"
    >
      <el-form-item label="用户名称" prop="jrname">
        <el-input v-model="ruleForm.jrname"></el-input>
      </el-form-item>
      <el-form-item label="地址名称" prop="jraddressesid">
        <el-select
          v-model="ruleForm.jraddressesid"
          placeholder="请选择地址名称"
        >
          <el-option
            v-for="(item, index) in addressname"
            :key="index"
            :label="item.addressname"
            :value="item.addressid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等级名称" prop="jrlevel">
        <el-select v-model="ruleForm.jrlevel" placeholder="请选择等级名称">
          <el-option
            v-for="(item, index) in levelname"
            :key="index"
            :label="item.levelname"
            :value="item.jrid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话号码" prop="jrtel">
        <el-input v-model="ruleForm.jrtel"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { saveOrUpdateMessage, getMessageById } from "@/api/message";
import { levelList } from "@/api/level";
import { getAddress } from "@/api/address";
export default {
  data() {
    return {
      ruleForm: {
        jrname: "",
        jraddressesid: "",
        jrlevel: "",
        jrtel: "",
      },
      rules: {
        jrname: [
          { required: true, message: "请输入用户名称", trigger: "change" },
          {
            min: 3,
            max: 5,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
        jraddressesid: [
          { required: true, message: "请选择地址名称", trigger: "change" },
        ],
        jrlevel: [
          { required: true, message: "请选择等级名称", trigger: "change" },
        ],
        jrtel: [
          { required: true, message: "请填写电话号码", trigger: "change" },
        ],
      },
      levelname: [],
      addressname: [],
    };
  },
  created() {
    this.level();
    this.address();
    if (this.$route.query.ids) {
      this.update();
    }
  },
  methods: {
    async level() {
      this.levelname = (await levelList()).data.records;
    },
    async address() {
      this.addressname = (await getAddress()).data.records;
    },
    message() {
      //创建的数据
      saveOrUpdateMessage({
        ...this.ruleForm,
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "成功添加",
            });
            console.log(res);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    update() {
      //获取message编辑的数据
      if (this.$route.query.ids) {
        console.log(this.$route.query.ids);

        getMessageById({
          jrid: this.$route.query.ids,
        })
          .then((res) => {
            if (res.code == 200) {
              this.ruleForm = res.data;
              console.log(this.ruleForm);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.$route.query.ids) {
            //数据修改
            saveOrUpdateMessage({
              ...this.ruleForm,
              jrid: this.$route.query.ids,
            })
              .then((res) => {})
              .catch((err) => {
                console.log(err);
              });
            this.$router.push("/message/messageList");
          } else {
            //加入数据
            this.message();
            this.$router.push("/message/messageList");
          }
        } else {
          console.log("error submit!!");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.main {
  width: 40%;
  margin: 120px auto;
}
</style>